<?php


namespace app\webactive\controller;


use app\common\lib\exception\ApiException;
use think\Controller;
use think\Exception;
use think\Log;

class Auth extends Controller
{
    protected $user = null;
    protected $isTest = false; // 是否是测试
    protected $noLoginActions = [];

    protected function _initialize()
    {
        parent::_initialize();

        $action = $this->request->action();

        if (!$this->isTest && !in_array($action, $this->noLoginActions, true))
        {   // 如果不是测试 并且 必须登陆
            try
            {
                $auth = new \app\api\controller\v2\Auth();
                $auth->checkRequestAuth();
                $this->user = $auth->user;
            }
            catch (Exception $e)
            {
                Log::write('活动请求头：' . \GuzzleHttp\json_encode($this->request->header(), JSON_UNESCAPED_UNICODE), 'error', true);
                Log::write('错误消息：' . $e->getMessage() . '错误文件' . $e->getFile() . '错误行数' . $e->getLine(), 'error', true);
                throw new ApiException('活动异常' . $e->getMessage());
            }

        } elseif ($this->isTest)
        {
            $this->user = [
                'id' => 12346160,
                'nickname' => 'lll'
            ];
        }
    }
}
